<?php
class DatabaseRecordSet implements Iterator {
	protected $resource;
	protected $size;
	protected $currentIndex;
	protected $currentObject;
	
	public function __construct($resource) {
		$this->resource = $resource;
		$this->size = mysql_num_rows($this->resource);
		$this->currentIndex	= 0;
		$this->currentObject = null;
	}
	
	public function __destruct() {
		@mysql_free_result($this->resource);
	}
	
	public function rewind() {
		$this->currentIndex = 0;
		$this->currentObject = null;
		@mysql_data_seek($this->resource, $this->currentIndex);
	}
	
	public function current() {
		if ( $this->currentObject == null ) {
			$this->currentObject = mysql_fetch_object($this->resource);
		}
		return $this->currentObject;
	}
	
	public function key() {
		return $this->currentIndex;
	}
	
	public function next() {
		if ( $this->currentIndex == $this->size ) {
			$this->currentObject = false;
		} else {
			$this->currentObject = mysql_fetch_object($this->resource);
			$this->currentIndex++;		
		}
		return $this->currentObject;
	}
	
	public function valid() {
		return ( $this->currentIndex < $this->size );
	}	

	public function toArray() {
		$array = array();
		foreach ( $this as $key => $value ) {
			$array[$key] = $value;
		}
		return $array;
	}
	/*	
	public function getValueAt($row, $field) {
		return @mysql_result($this->resource, $row, $field);
	}*/
}

?>